<?php
namespace app\common\logic;
class Send
{
    /**
     * 发送验证类验证码
     * @param string $telephone  接收短信的电话
     * @param string $code       验证码
     * @param string $auth       接收类型
     * @return array|bool
     */
    public function code($telephone= '',$code='',$auth='',$style = 1){
        $cacheName = $telephone.$auth;
        if(checkTel($telephone) !== true)
            return checkTel($telephone);

        if ($code =='')
            return [
                'message' => '验证码不能为空'
            ];

        //一分钟后再试
        if ((time()-cache($cacheName)['time'])<60)
            return [
                'message' => '发送过于频繁'
            ];

        if (cache($cacheName)['num'] >= 5)
            return [
                'message' => '验证次数过多，请一小时后再试'
            ];
        switch ($style){
            case 1:
                $codeMode = 'SMS_137350251';
                break;
                case 2:
                $codeMode = 'SMS_150739285';
                break;
            default:
                $codeMode = 'SMS_137350251';
                break;
        }
        \alisend\api_demo\SmsDemo::sendSms($telephone,['code'=>$code],$codeMode);

        $data['code'] = $code;
        $data['time'] = time();
        cache($cacheName)?$data['num']=cache($cacheName['num']):$data['num']=0;
        cache($cacheName,$data,3600);
        return [
            'message' => '发送成功',
            'code' => '1'
        ];
    }

    /**
     * 通知类短信-报备
     * @param $telephone
     * @param $origin
     * @param $name
     * @param $telephone
     * @return array
     */
    public function inform($telephone,$origin,$name,$attach){
        \alisend\api_demo\SmsDemo::sendSms($telephone,['origin'=>$origin,'name'=>$name,'telephone'=> $attach,'time'=>date('Y-m-d H:i:s')],'SMS_139239991');
        return [
            'message' => '发送成功',
            'code' => '1'
        ];
    }

    /**
     * 通知类短信-转介/撤回
     * @param $telephone
     * @param $origin
     * @param $name
     * @param $telephone
     * @return array
     */
    public function inform_transfer($telephone,$name,$user,$type = true){
        $model = $type?'SMS_145290802':'SMS_145290806';
        \alisend\api_demo\SmsDemo::sendSms($telephone,['name'=>$name,'telephone'=> $user],$model);
        return [
            'message' => '发送成功',
            'code' => '1'
        ];
    }
}